#!/usr/bin/python
# encoding:utf-8

# ****************************************************************
# Description： case处理区
# Author： jf
# Date：20170605
# version：
# ****************************************************************

from TestFrame import *
from PySerial_relase1 import *
import mod_config

def case_run (suiteid, comapp):
	print u'【' + suiteid + u'】' + ' Test Begin,please waiting...'
	# global mailbody

	# 清除历史测试数据
	excelobj.del_testrecord(suiteid)

	# 清除上一个测试模块计数
	#使用读取excel中统计结果，此处不再需要原有测试结果
	#clearcount(excelobj)

	# 获取当前版本号：
	version = getversion(comapp)
	versionOut(version)
	excelobj.write_ver(suiteid, version)

	# 获取case个数
	casecount = excelobj.get_ncase(suiteid)

	#mailbody = []
	# 遍历执行case
	for caseid in range(0, casecount):
		# print 'caseid:', caseid, 'casecount:', casecount

		# 检查测试用例执行级别
		testlevel = mod_config.getConfig("CFG","testlevel")
		if testlevel != "ALL":
			if excelobj.read_data(suiteid, excelobj.casebegin + caseid, excelobj.testlevel).upper() != "P0":
				continue

		# 检查是否执行该Case
		# 如果run列填写为N，则该用例不执行，测试结果直接写NT
		if excelobj.read_data(suiteid, excelobj.casebegin + caseid, excelobj.testcontrol).upper() == 'N':
			# excelobj.write_data(suiteid, excelobj.casebegin+caseid, excelobj.resultCol, 'NT', NT_COLOR)
			# print 'caseid:', caseid, 'casecount:', casecount
			ret1 = 'NT'
			# 写测试结果
			write_result(excelobj, suiteid, caseid, excelobj.resultCol, ret1)

			continue  # 当前Case结束，继续执行下一个Case
		else:
			# 1.读取excel要发送的AT命令行
			cmdsent = excelobj.read_data(suiteid, excelobj.casebegin + caseid, excelobj.argbegin)
			# print '发送命令行：',cmdsent

			# 2.发送AT命令并处理返回值
			Timedelay = excelobj.read_data(suiteid, excelobj.casebegin + caseid, excelobj.TimeDelaycol)
			if Timedelay == "None":
			#print Timedelay
				Timedelay = 0.5
			sReal = SendAT(comapp, cmdsent, float(Timedelay))
			# 测试结果写入实际返回值列
			excelobj.write_data(suiteid, excelobj.casebegin + caseid, excelobj.RealCol, sReal, PASS_COLOR)

			# 3.检测返回结果与预期结果比较,结果写入Excel
			# 获取预期结果的key字段
			sExpect = (excelobj.read_data(suiteid, excelobj.casebegin + caseid, excelobj.CheckVaule))
			#print sExpect
			if sExpect == "None":
				ret1 = u"手工检查项目"
				excelobj.write_data(suiteid, excelobj.casebegin + caseid, excelobj.attentionCol, ret1.encode("gbk"), PASS_COLOR)
			#对预期结果和真是结果进行比较处理
			ret = process_result(sReal, sExpect, cmdsent)
			#ret = assert_result(sReal, sExpect)		#删除老的比较函数
			if ret is 'PASS':
				# 测试pass
				write_result(excelobj, suiteid, caseid, excelobj.resultCol, ret)
			else:
				# 测试结果fail
				# 写测试结果
				write_result(excelobj, suiteid, caseid, excelobj.resultCol, ret)

	print u'【' + suiteid + u'】' + ' Test End!' + '\n' + '*' * 70
	#count = statisticresult(excelobj)
	#通过excel更新测试结果，结果对应实际测试用例个数
	excelobj.sum_result(suiteid)
	count = statisticresult2(excelobj,suiteid)
	#return mailbody, count
	return count

